var db = require("../db/index");
var config = require("../_include/config");
var helper = require("../_include/helper");
var db_table_name = config.dbname.district;
var db_id_prefix = config.dbIdPrefix.district;
//区域对象
var _con = function(info) {
	var self = this;
	if (!info.province) {
		throwCusError("省级名字不可为空");
	}
	self.province = info.province;
	self.town = info.town;
	self.county = info.county;
};

module.exports = _con;
// 初始化数据
// 数据从db的city表中映射而来
if (!db.find_all(db_table_name).length) {
	var city_list = require("../_include/city/city.json");
	var city_map = {};
	var base_uid_map = Object.create(null);

	//数据关系树化
	city_list.forEach(function(item) {
		city_map[item.region_id] = item;
	});

	//根据parent_id生成对应的base_id
	//base_id >= 1
	city_list.forEach(function(item) {
		var base_id = ~~base_uid_map[item.parent_id] + 1;
		city_map[item.region_id].base_id = base_id;
	});

	//把base_id连接起来连城parent_id
	//把parent_id转化成完整district_id
	//写入数据库
	city_list.forEach(function(item) {
		var parent = city_map[item.parent_id];
		if (parent) {
			var parent_id = parent.parent_id;
		}
		item.parent_id = String(parent_id || "") + item.base_id;
		item.district_code = (item.parent_id + "000000").substr(0, 6);
		db.insert(db_table_name, {
			_id: item.region_name,
			region_id: item.region_id,
			district_code: item.district_code
		});
	});
};

//将局域对象转化为一个有规律的数字
//一共6位数 **, **, **
//分别代表省, 市, 县区[, 街道]
//其中00代表空
/*
 * 所以同省就有差值小于1,00,00
 * 同市区差值小于1,00
 */
_con.prototype.valueOf = function() {

};